<?php

//防止恶意调用
if (!defined('IN_MOD'))
{
    die('Hacking attempt');
}

// 验证会员登录
getmemberlogin($configmember);

// 购物列表
$cartdatalist=getcartdatalist();
$cartlist = $cartdatalist['cartlist'];
$cartshoplist = $cartdatalist['cartshoplist'];
$shopcount = $cartdatalist['shopcount'];
if(empty($cartlist))moduleerror(array("message"=>"购物车中没有商品"));
$this->smarty_assign('cartlist',     $cartlist);
foreach($cartshoplist as $shopid=>$val)
{
	// 当前店铺活动列表
	$activitydatalist = getorderactivitylist($shopid);
	$activitylist = $activitydatalist['list'];
	$cartshoplist[$shopid]['activitylist'] = $activitylist;
}
$this->smarty_assign('cartshoplist',     $cartshoplist);

// 购物车中的积分兑换商品
$integralgoodsgroup = $cartdatalist['integralgoodsgroup'];
$this->smarty_assign('integralgoodsgroup',     $integralgoodsgroup);

$modulehook = "api";$hookname = "pay";require(MODULEPARA_PATH . 'config/modulehook.php');// 调用系统支付插件钩子

$act = moduleaqvarval("act","string");

// 订单结算
if($act == "done")
{
	$configinfo['module'][$configinfo['moduleid']]['author'] = "flow_done";
	
	// 地址列表
	$modulewhere=array();
	$modulewhere["tablename"]="modulepara_address";
	$modulewhere["arrayclass"]=$configmember['id'];
	$modulewhere["bigclassname"]="userid";
	$modulewhere["order"]='id DESC';
	$modulewhere["langallok"]=1;
	$addresslist=modulepara("configmodule",$modulewhere,1);
	$this->smarty_assign('addresslist',     $addresslist);
	
	// 默认地址
	$modulewhere=array();
	$modulewhere["tablename"]="modulepara_address";
	$modulewhere["dataid"]=$configmember['id'];
	$modulewhere["dataidname"]="userid";
	$modulewhere["where"]="defok = '1'";
	$taddress=modulepara("datainfo",$modulewhere);
	$this->smarty_assign('taddress',     $taddress);
	
	// 可使用优惠券列表
	$shopmemberusecouponlist = array();
	foreach($cartshoplist as $shopid => $shopdata)
	{
		$couponlist = getmemberusecouponlist($shopid);
		if(!empty($couponlist))
		{
			$shopmemberusecouponlist[$shopid]['id'] = $shopdata['id'];
			$shopmemberusecouponlist[$shopid]['shop_name'] = $shopdata['shop_name'];
			$shopmemberusecouponlist[$shopid]['list'] = $couponlist;
		}
	}
	$this->smarty_assign('memberusecouponlist',     $shopmemberusecouponlist);
	
	// 可使用积分抵扣总数
	$integraldknumber = getintegraldknumber();
	$integralmoney = $integraldknumber['integralmoney'];// 积分抵扣总价格
	$integralnumber = $integraldknumber['integralnumber'];// 积分抵扣总数量
	$this->smarty_assign('integralmoney',     $integralmoney);
	$this->smarty_assign('integralnumber',     $integralnumber);
	
	// 获取商品优惠活动价格信息
	$activitylist = getorderactivitylist();
	$this->smarty_assign('activitylist',     $activitylist['list']);
	$this->smarty_assign('activityprice',     $activitylist['price']);
	$this->smarty_assign('shoptotalprice',     $activitylist['goodsprice']);
	$this->smarty_assign('shopoldtotalprice',     $activitylist['goodsoldprice']);
	
// 订单提交
}else if($act == "ordersubmit")
{
	// 当前购物车中商品为积分兑换商品时使用权限判断
	if(!empty($integralgoodsgroup) && $integralgoodsgroup['integralgoods'] == 1)
	{
		// 兑换商品时一次只能有一个商品，大于1个时非法操作
		if($integralgoodsgroup['number'] > 1)
		{
			moduleerror(array("message"=>"兑换数量不合法，非法操作！"));		
		}
		// 判断会员积分是否足够
		if($configmember['score'] < $integralgoodsgroup['price'])
		{
			moduleerror(array("message"=>"您的积分不足，无法订购！当前积分：".$configmember['score']));	
		}
	}
			
	$moduleflow=new moduleflow();
	
	$configinfo['module'][$configinfo['moduleid']]['author'] = "flow_ok";
	
	$orderaddress = moduleaqvarval("orderaddress","intval");
	$payfs = moduleaqvarval("payfs","intval");
	$psfs = moduleaqvarval("psfs","string");
	$couponids = moduleaqvarval("couponids","array");
	$shoppayapi = $configinfo['shoppayapi'][$payfs];
	$psname = $configbase['shopconfig']['wllist'][$psfs][0];
	$psfee = $configbase['shopconfig']['wllist'][$psfs][1];
	$useintegral = moduleaqvarval("useintegral","intval");
	$postdesc = moduleaqvarval("postdesc","string");
	if(empty($orderaddress))moduleerror(array("message"=>"请选择一个收货地址"));
	if(empty($payfs))moduleerror(array("message"=>"请选择一个支付方式"));
	if(empty($psfs))moduleerror(array("message"=>"请选择一个配送方式"));
	
	// 可使用积分抵扣总数权限判断
	$integraldknumber = getintegraldknumber();
	$integralnumber = $integraldknumber['integralnumber'];// 积分抵扣总数量
	if($useintegral > $configmember['score'] || $useintegral > $integralnumber)
	{
		moduleerror(array("message"=>"使用积分不合法，非法操作！"));	
	}
	
	// 地址信息
	$modulewhere=array();
	$modulewhere["tablename"]="modulepara_address";
	$modulewhere["dataid"]=$configmember['id'];
	$modulewhere["dataidname"]="userid";
	$modulewhere["where"]="id = '".$orderaddress."'";
	$taddress=modulepara("datainfo",$modulewhere);
	
	// 创建店铺订单
	$totalordersn = $moduleflow->flow_getordersn();
	$this->smarty_assign('ordersn',     $totalordersn);
	$ordertotalprice = 0;
	foreach($cartshoplist as $shopid => $shopdata)
	{
		// 购买商品赠送积分数量
		$shopgiveintegralnumber = getgiveintegralnumber($shopid);
		
		// 计算使用优惠券费用
		$memberusecouponlist = getmemberusecouponlist($shopid);// 可使用优惠券列表
		// 当前使用所有优惠券组，每个店铺限用一张优惠券
		foreach($couponids as $key=>$couponid)
		{
			if($memberusecouponlist[$couponid])
			{
				$myyhjinfo = $memberusecouponlist[$couponid];// 获取当前可使用的优惠券信息
			}
		}
		$shopcouponfee = $myyhjinfo ? $myyhjinfo['yhmoney'] : 0;// 优惠券金额
		
		// 计算积分抵扣金额
		$shopintegraldknumber = getintegraldknumber($shopid);
		$shopintegraldk100 = $shopintegraldknumber['integralnumber'] / $integraldknumber['integralnumber'];// 占可抵扣总积分百分比
		$shopuseintegral = $useintegral * $shopintegraldk100;// 获取当前店铺抵扣积分数量
		
		// 店铺平均分配运费
		$shoppsfee = $psfee / $shopcount;
	
		// 订单总金额
		$activitylist = getorderactivitylist($shopid,array('yhjfee'=>$shopcouponfee),array('psfee'=>$shoppsfee,'useintegral'=>$shopuseintegral));
		$ordertotalprice += $activitylist['price'];
		
		// 设置数据	
		$moduleaqvar = array();
		$moduleaqvar[0] = 'senddata-module';
		$moduleaqvar[1] = 'senddata-class2';
		$moduleaqvar[2] = 'senddata-class3';
		$moduleaqvar[3] = 'senddata-create_time';
		$moduleaqvar[4] = 'senddata-published_time';
		$moduleaqvar[5] = 'senddata-category_id';
		$moduleaqvar[6] = 'senddata-lang';
		$moduleaqvar[7] = 'senddata-user_id';
		$moduleaqvar[8] = 'senddata-user_name';
		$moduleaqvar[9] = 'senddata-post_status';// 设置留言默认未处理状态
		$moduleaqvar[10] = 'senddata-supplier_id';
		$moduleaqcontent = array();
		$moduleaqcontent[0] = $configinfo['moduleid'];
		$moduleaqcontent[1] = 0;
		$moduleaqcontent[2] = 0;
		$moduleaqcontent[3] = time();
		$moduleaqcontent[4] = time();
		$moduleaqcontent[5] = 109;
		$moduleaqcontent[6] = $lang;
		$moduleaqcontent[7] = $configmember['id'];
		$moduleaqcontent[8] = $configmember['user_login'];
		$moduleaqcontent[9] = 0;// 设置留言默认未处理状态
		$moduleaqcontent[10] = $shopid;
		moduleaqvardata($moduleaqvar,$moduleaqcontent);
	
		// 提交订单
		$orderstatus_name = "orderstatus";
		$integralok_name = "integralok";
		$totalordersn_name = "totalordersn";
		$giveintegral_name = "giveintegral";
		$useyhj_name = "useyhj";
		$ordersn = $moduleflow->flow_getordersn();
		$moduledata = array();
		$moduledata = moduleaqvarval('senddata','array');
		$moduledata['post_title'] = $ordersn;
		$moduledata[$orderstatus_name] = 0;// 订单状态
		if(!empty($integralgoodsgroup['integralgoods']) && $integralgoodsgroup['integralgoods'] == 1)$moduledata[$integralok_name] = 1;// 兑换商品
		$moduledata[$totalordersn_name] = $totalordersn;// 总订单号
		if($shopgiveintegralnumber)$moduledata[$giveintegral_name] = $shopgiveintegralnumber;// 赠送积分数量
		if($myyhjinfo)$moduledata[$useyhj_name] = $myyhjinfo['id'];// 使用优惠券
		$insert_order_id = insertdata('fastxcms_portal_post_order',$moduledata);
		updatedata('fastxcms_portal_post_order',"`update_time` = '".time()."'",$configinfo['article_id'],$insert_order_id);
		
		// 设置参数
		global $getdataparameter;
		$getdataparameter['parameter']['price'] = $activitylist['price'];// 应收款 订单金额(含运费)
		$getdataparameter['parameter']['orderprice'] = $activitylist['orderprice'];// 订单金额
		$getdataparameter['parameter']['goodsprice'] = $activitylist['goodsprice'];// 商品金额
		$getdataparameter['parameter']['discountprice'] = $activitylist['discountprice'];// 折扣金额
		$getdataparameter['parameter']['payid'] = $shoppayapi['id'];
		$getdataparameter['parameter']['payname'] = $shoppayapi['name'];
		$getdataparameter['parameter']['payindex'] = $shoppayapi['index'];
		$getdataparameter['parameter']['psname'] = $psname;
		$getdataparameter['parameter']['couponfee'] = $shopcouponfee;// 使用优惠券
		$getdataparameter['parameter']['psfee'] = $shoppsfee;// 配送费用
		$getdataparameter['parameter']['dkprice'] = $activitylist['dkprice'];// 积分抵扣
		$getdataparameter['parameter']['postdesc'] = $postdesc;// 订单备注
		if(!empty($integralgoodsgroup['integralgoods']) && $integralgoodsgroup['integralgoods'] == 1)$getdataparameter['parameter']['integralprice'] = $integralgoodsgroup['integralprice'];// 积分兑换价格
		$getdataparameter['parameter']['contact_name'] = $taddress['name'];
		$getdataparameter['parameter']['contact_tel'] = $taddress['tel'];
		$getdataparameter['parameter']['contact_address'] = $taddress['address'];
		
		insertmoduleparameter($insert_order_id, 109, 109);
		
		// 插入订单商品
		foreach($shopdata['list'] as $key=>$val)
		{
			// 统计商品赠送积分
			$giveintegral = $val['giveintegral'] == -1 ? $val['goodsprice'] : $val['giveintegral'];// 赠送积分为-1时按商品价格赠送
			$goodstotalintegral = $giveintegral * $val['number'];// 赠送积分总数
			
			$moduledata = array();
			$moduledata['orderid'] = $insert_order_id;
			$moduledata['listid'] = $val['listid'];
			$moduledata['listcatid'] = $val['listcatid'];
			$moduledata['userid'] = $val['userid'];
			$moduledata['goods_name'] = $val['goods_name'];
			$moduledata['goods_img'] = $val['goods_img'];
			$moduledata['price'] = $val['price'];
			$moduledata['number'] = $val['number'];
			$moduledata['goods_sn'] = $val['goods_sn'];
			$moduledata['goods_attr'] = $val['goods_attr'];
			$moduledata['giveintegral'] = $goodstotalintegral;// 赠送积分
			$moduledata['supplier_id'] = $shopid;
			$moduledata['lang'] = $val['lang'];
			insertdata('modulepara_order_goods',$moduledata);	
		}
	}
	
	// 订单总价格
	$this->smarty_assign('totalprice',     $ordertotalprice);
	
	// 兑换商品时设置直接操作会员积分
	if(!empty($integralgoodsgroup['integralgoods']) && $integralgoodsgroup['integralgoods'] == 1)
	{
		// 更新会员帐户积分
		updatemembermoney($configmember['id'],$integralgoodsgroup['price'],2,'-');
			
		// 获取所有店铺的订单
		$modulewhere=array();
		$modulewhere["tablename"]="fastxcms_portal_post_order";
		$modulewhere["arrayclass"]=$totalordersn;
		$modulewhere["bigclassname"]="totalordersn";
		$modulewhere["order"]='default';
		$shoporderlist=modulepara("configmodule",$modulewhere,1);
		foreach($shoporderlist as $key => $val)
		{			
			// 修改订单状态支付成功
			order_paid($val['id'], 1);
		}
	}
	
	// 清空购物车
	deletedata("modulepara_cart", "userid", $configmember['id']);
	
}else{
	// 开启MVC框架模式
	if($configinfo['mvc'] == 1)
	{
		$this->smarty_assign('cartlist',     $cartlist);
	}else{
		smarty_assign('cartlist',     $cartlist);	
	}
}
?>